//
//  ShowOrderVC.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/28.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "ShowOrderVC.h"
#import "MJRefresh.h"
#import "ShowOrderModel.h"
#import "ShowTimeCell.h"
#import "ShowDetailVC.h"
#import "NoDataView.h"
//晒单列表
#define SHAIDAN_LIST_URL @"/api/ApiGoods/getShoppingShare"
@interface ShowOrderVC ()<UITableViewDataSource,UITableViewDelegate>
{
    //列表
    UITableView *_tableView;
    //数据源
    NSMutableArray *_data;
    NSInteger _currentPage;
}
@end

@implementation ShowOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _data = [NSMutableArray array];
    _currentPage = 1;
    [self loadCustomNaviBar];
    [self loadTableView];
    [self startRequestForData];
}

//加载导航条
- (void)loadCustomNaviBar
{
    UIView *naviBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WYMWITH, 20+44)];
    naviBar.backgroundColor = QBB_RED_COLOR;
    //返回按钮
    PublicBackBtn *backBtn = [[PublicBackBtn alloc]initWithVC:self andBackType:pop Img:[UIImage imageNamed:@"icon_back_white_press"]];
    [naviBar addSubview:backBtn];
    //标题
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 25)];
    titleLab.center = CGPointMake(WYMWITH/2, 42);
    if(self.isDetailFlag){
        titleLab.text = @"晒单分享";
    }else{
        titleLab.text = @"晒单记录";
    }
    titleLab.font = [UIFont systemFontOfSize:TITLE_SIZE];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = [UIColor whiteColor];
    //分割线
    UILabel *fenge = [[UILabel alloc]initWithFrame:CGRectMake(0, 63.5, WYMWITH, 0.5)];
    fenge.backgroundColor = [UIColor lightGrayColor];
    [naviBar addSubview:fenge];
    [naviBar addSubview:titleLab];
    [self.view addSubview:naviBar];
}
//加载表格
- (void)loadTableView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WYMWITH, WYMHEIGHT - 64)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    //注册cell
    [_tableView registerNib:[UINib nibWithNibName:@"ShowTimeCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableView.rowHeight = 180.0;
    _tableView.tableFooterView = [[UIView alloc]init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [self setupRefresh];
}
/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    //    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    // dateKey用于存储刷新时间，可以保证不同界面拥有不同的刷新时间
    [_tableView addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [_tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    _currentPage = 1;
    [_data removeAllObjects];
    [self startRequestForData];
}

- (void)footerRereshing
{
    _currentPage++;
    [self startRequestForData];
}
//启动数据请求
- (void)startRequestForData
{
    [WYPublic addHudToWindow];
    NSDictionary *params = nil;
    if (self.isDetailFlag) {
        if ([self.pdc_sid class] ==[NSNull class]) {
            return;
        }else{
            if (self.pdc_sid.length == 0) {
                return;
            }
        }
        params = @{@"sid":self.pdc_sid,@"num":@"20",@"page":[NSString stringWithFormat:@"%ld",(unsigned long)_currentPage]};
    }else{
        //是否请求未审核信息标志
        NSString *isFlag = @"0";
        if ([_userId isEqualToString:[UserInfo getUserID]]) {
            isFlag = @"1";
        }
        params = @{@"uid":_userId,@"is_fetch_all":isFlag,@"num":@"20",@"page":[NSString stringWithFormat:@"%ld",(unsigned long)_currentPage]};
    }
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,SHAIDAN_LIST_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        if ([ret integerValue] == 0) {
            NSArray *list = dic[@"data"];
            for (NSDictionary *sd in list) {
                ShowOrderModel *model = [[ShowOrderModel alloc]init];
                model.sd_content = sd[@"sd_content"];
                model.sd_id = sd[@"sd_id"];
                model.sd_imgs = sd[@"sd_thumbs_url"];
                model.sd_pdc_id = sd[@"id"];
                model.sd_user_img = sd[@"img_url"];
                model.sd_pdc_qishu = sd[@"qishu"];
                model.sd_pdc_sid = sd[@"sid"];
                model.sd_pdc_title = sd[@"title"];
                model.sd_time = sd[@"sd_time"];
                model.sd_title = sd[@"sd_title"];
                model.sd_uid = sd[@"uid"];
                model.sd_username = sd[@"username"];
                model.sd_is_passed = sd[@"sd_is_passed"];
                [_data addObject:model];
            }
            if (_data.count == 0) {
                [self addNODataView];
            }
            [_tableView reloadData];
        }else{
            [WYPublic showMessageWithTitle:@"" Msg:dic[@"msg"]];
        }
        [WYPublic removeHudToWindow];
        [_tableView headerEndRefreshing];
        [_tableView footerEndRefreshing];
    } failedBlock:^{
        [WYPublic removeHudToWindow];
        [_tableView headerEndRefreshing];
        [_tableView footerEndRefreshing];
    } needLog:NO];
}
//没有数据默认填充的控件
- (void)addNODataView{
    NoDataView *nodateView = [[[NSBundle mainBundle]loadNibNamed:@"NoDataView" owner:self options:nil]lastObject];
    nodateView.frame = CGRectMake(0, 64, WYMWITH, nodateView.frame.size.height);
    if (self.isMyRecordFlag) {
        nodateView.isMineFlag = YES;
        nodateView.title = @"您还没有晒单记录哦!";
    }else{
        nodateView.isMineFlag = NO;
        if (self.isDetailFlag) {
            nodateView.title = @"小伙伴们还没有晒单记录哦!";
        }else{
            nodateView.title = @"ta还没有晒单记录哦!";
        }
    }
    [self.view addSubview:nodateView];
}

#pragma mark -- 表格协议函数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShowTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (_data.count > 0) {
        ShowOrderModel *model = _data[indexPath.row];
        cell.isDetailFlag = self.isDetailFlag;
        [cell fillCellWithModel:model];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_data.count > 0) {
        ShowOrderModel *model = _data[indexPath.row];
        //跳往晒单详情
        ShowDetailVC *showDetailVC = [[ShowDetailVC alloc]init];
        showDetailVC.model = model;
        [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:showDetailVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [WYPublic removeHudToWindow];
}

- (void)dealloc{
    
    NSLog(@"ShowOrderVC dealloc");
}

@end
